/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bd;
import classes.*;
import java.sql.*;
import java.util.*;
/**
 *
 * @author Alan JPS
 */
public class StatusChamadoBD {

    public static boolean salvar(StatusChamado statusChamado,Usuario usuarioLogado)
    {
        try{
            Connection conexao = Conexao.abrir();
            CallableStatement procedure = conexao.prepareCall("{call AT_STATUSCHAMADO(?,?,?,?,?)}");
            procedure.setInt(1, (Integer)null);
            procedure.setString(2,statusChamado.getDescr());
            procedure.setInt(3,usuarioLogado.getId());
            procedure.setInt(4,(Integer)null);
            procedure.setString(5,"IN");

            procedure.execute();
            procedure.close();
            conexao.close();
            return true;

        }catch(Exception excecao)
        {
            return false;
        }
    }

    public static boolean alterar(StatusChamado statusChamado,Usuario usuarioLogado)
    {
        try{
            Connection conexao = Conexao.abrir();
            CallableStatement procedure = conexao.prepareCall("{call AT_STATUSCHAMADO(?,?,?,?,?)}");
            procedure.setInt(1, statusChamado.getId());
            procedure.setString(2,statusChamado.getDescr());
            procedure.setInt(3,(Integer)null );
            procedure.setInt(4, usuarioLogado.getId());
            procedure.setString(5,"AL");

            procedure.execute();
            procedure.close();
            conexao.close();
            return true;

        }catch(Exception excecao)
        {
            return false;
        }
    }

    public static boolean deletar(int codigo)
    {
        try{
            Connection conexao = Conexao.abrir();
            CallableStatement procedure = conexao.prepareCall("{call AT_STATUSCHAMADO(?,?,?,?,?)}");
            procedure.setInt(1, codigo);
            procedure.setString(2,null);
            procedure.setInt(3,(Integer)null );
            procedure.setInt(4, (Integer)null);
            procedure.setString(5,"");

            procedure.execute();
            procedure.close();
            conexao.close();
            return true;
        }catch(Exception excecao)
        {
            return false;
        }
    }

    public static StatusChamado selecionar(int codigo)
    {
        StatusChamado retorno = null;
        try{
            Connection conexao = Conexao.abrir();
            Statement comandoSelect = conexao.createStatement();
            ResultSet resultado = comandoSelect.executeQuery("select * from VIEW_SELECIONASTATUSCHAMADO where sta_id="+codigo+"");
            if(resultado.next())
            {
                StatusChamado novoStatusChamado = new StatusChamado();
                novoStatusChamado.setId(codigo);
                novoStatusChamado.setDescr(resultado.getString("sta_descr"));
                retorno=novoStatusChamado;
            }
            conexao.close();
            return retorno;
        }catch(Exception excecao)
        {
            return null;
        }
    }

    public static ArrayList<StatusChamado> selecionar()
    {
        ArrayList<StatusChamado> vetStatusChamados = new ArrayList<StatusChamado>();
        try{
            Connection conexao = Conexao.abrir();
            Statement comandoSelect = conexao.createStatement();
            ResultSet resultado = comandoSelect.executeQuery("select * from VIEW_SELECIONASTATUSCHAMADO");
            while(resultado.next())
            {
                StatusChamado novoStatusChamado = new StatusChamado();
                novoStatusChamado.setId(resultado.getInt("sta_id"));
                novoStatusChamado.setDescr(resultado.getString("sta_descr"));
                vetStatusChamados.add(novoStatusChamado);
            }
            conexao.close();
            return vetStatusChamados;
        }catch(Exception excecao)
        {
            return null;
        }
    }

}
